<?php

/**
 * This is the model class for table "{{admin_info}}".
 *
 * The followings are the available columns in table '{{admin_info}}':
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property string $fullname
 * @property integer $modifieddate
 */
class AdminInfo extends CActiveRecord
{
	public $old_password;
	public $new_password;
	public $re_password;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return AdminInfo the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{admin_info}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, fullname, yahoo', 'required'),
			array('old_password', 'validateOldPassword'),
			array('new_password', 'validatePassword'),
			/*array('modifieddate', 'numerical', 'integerOnly'=>true),
			array('username, password', 'length', 'max'=>50),
			array('fullname', 'length', 'max'=>200),*/
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, username, password, fullname, modifieddate', 'safe', 'on'=>'search'),
		);
	}
  
  public function validateOldPassword()
  {
    if (!empty($this->old_password) && md5($this->old_password) != $this->password) {
      $this->addError('old_password', Yii::t('default', 'Old Password must be repeated exactly.'));
      return false;
    }
    return true;
  }
	
	public function validatePassword()
	{
	  if (!empty($this->new_password)) {
      if (empty($this->re_password)) {
        $this->addError('re_password', Yii::t('default', 'Please type Confirm New Password.'));
        return false;
      }
      if ($this->re_password != $this->new_password) {
        $this->addError('re_password', Yii::t('default', 'Confirm Password must be repeated exactly.'));
        return false;
      }
    }
    elseif (!empty($this->old_password)) {
      $this->addError('new_password', Yii::t('default', 'Please type New Password.'));
      return false;
    }
	  /*if (!empty($this->old_password) || (!empty($this->new_password) || !empty($this->re_password))) {
  	  if (empty($this->old_password) && (!empty($this->new_password) || !empty($this->re_password))) {
  	    $this->addError('old_password', Yii::t('default', 'Please type old password.'));
  	    return false;
  	  }
  	  else {
  	    if ($this->old_password != $this->password) {
  	      $this->addError('old_password', Yii::t('default', 'Old password is not correct.'));
  	      return false;
  	    }
  	    else {
      	  if (!empty($this->new_password)) {
            if (empty($this->re_password)) {
              $this->addError('re_password', Yii::t('default', 'Please type Confirm New Password.'));
              return false;
            }
            if ($this->re_password != $this->new_password) {
              $this->addError('re_password', Yii::t('default', 'Confirm Password must be repeated exactly.'));
              return false;
            }
          }
  	    }
  	  }
	  }*/
	  
	  return true;
	} 
	
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
      'id'           => 'ID',
      'username'     => Yii::t('default', 'Username'),
      'password'     => Yii::t('default', 'Password'),
      'old_password' => Yii::t('default', 'Old Password'),
      'new_password' => Yii::t('default', 'New Password'),
      're_password'  => Yii::t('default', 'Confirm New Password'),
      'fullname'     => Yii::t('default', 'Full Name'),
  		'yahoo'        => 'Yahoo chat',
      'modifieddate' => Yii::t('default', 'Last Modifieddate'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('fullname',$this->fullname,true);
		$criteria->compare('modifieddate',$this->modifieddate);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}